<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
class Invite extends Controller{
    public function invite_list(){
    	if(!session('invitephone')){
            $this->redirect(url('login'));
        }else{
	        $key=input('get.key');
            if (!empty(input('get.fromtime'))) {
                $fromtime=strtotime(input('get.fromtime'));
            }else{
                $fromtime=0;
            }
            if (!empty(input('get.totime'))) {
                $totime=strtotime(input('get.totime'))+86400;
            }else{
                $totime=time();
            }
		        $list=Db::name('users')->where('time','>',"{$fromtime}")->where('time','<',"{$totime}")->where('name|phone','like',"%{$key}%")->where('invitephone',session('invitephone'))->order('id desc')->paginate(10);
                if ($list) {
                    $items = $list->items();
                    foreach ($items as $k => $v){ 
                        $items[$k]['phone']=substr_replace($v['phone'],'****',3,4);
                    }
                }
		        $lists=Db::name('users')->where('time','>',"{$fromtime}")->where('time','<',"{$totime}")->where('name|phone','like',"%{$key}%")->where('invitephone',session('invitephone'))->select();
	        
	        $this->assign('key',input('get.key'));
	        $this->assign('fromtime',input('get.fromtime'));
	        $this->assign('totime',input('get.totime'));
		    $this->assign('list',$list);
		    $this->assign('lists',$lists);
            $this->assign('items',$items);
        	return $this->fetch();
        }
    }
    public function login(){
    	return $this->fetch();
    }
    public function doLogin(){
    	$invitephone = input("param.invitephone");
        $password = input("param.password");
        $verifycode = input("param.verifycode");
        $hasUser = Db::name('channel')->where(['phone'=> $invitephone,'state'=>1])->find();
        if(empty($hasUser)){
            return json(['code' => 501, 'url' => '', 'msg' => '用户不存在或已禁用']);
        }elseif($password!=$hasUser['password']){
            return json(['code' => 502, 'url' => '', 'msg' => '密码错误']);
        }elseif(!captcha_check($verifycode)){
        	return json(['code' => 503, 'url' => '', 'msg' => '验证码错误']);
		}else{
        	session('invitephone', $invitephone);
        	return json(['code' => 200, 'url' => url('invite_list'), 'msg' => '成功登陆']);
        }
    }
    public function loginOut(){
    	session('invitephone', null);
        return json(['code' => 200, 'url' => url('login'), 'msg' => '成功登出']);
    }
}
